package ru.isys.yoomee.ui;

import java.util.ArrayList;

import ru.isys.yoomee.R;
import ru.isys.yoomee.model.Chat;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ChatsAllActivity extends ListActivity
{
    ArrayList<Chat> mList = new ArrayList<Chat>();
    public final static String CHATS_ALL_CHAT = "chats_all_chat";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        Bundle extras;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chats_all_layout);
        if ((extras = getIntent().getExtras()) == null)
        {
            mockChats();
            
        }
        else
        {
            mList = (ArrayList<Chat>) extras.getSerializable(CHATS_ALL_CHAT);
           

        }
        getListView().setAdapter(new ChatsAdapter(this, R.layout.chats_all_list_item_chat_with_one_yoo, mList));
        
    }

    private void mockChats()
    {
        mList.add(new Chat("Nickname", "SDfafgrgrawgawrg"));
        mList.add(new Chat("Nickname2", "SDfafgrgrawgawrg"));
        mList.add(new Chat("Nickname3", "SDfafgrgrawgawrg"));
        mList.add(new Chat("Nickname4", "SDfafgrgrawgawrg"));
        mList.add(new Chat("Nickname5", "SDfafgrgrawgawrg"));
        mList.add(new Chat("Nickname6", "SDfafgrgrawgawrg"));

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.yoomee_menu_layout, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle item selection
        switch (item.getItemId())
        {
            case R.id.mee_config:
                startMeeConfigActivity();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void startMeeConfigActivity()
    {
        startActivity(new Intent(this, MeeActivity.class));
    }

    private class ChatsAdapter extends ArrayAdapter<Chat>
    {

        private ArrayList<Chat> mListOfChats;
        private View mView;
        private LayoutInflater mLayoutInflater;
        private Chat mChat;
        private TextView mNickname;
        private TextView mSomeChatText;
        private ImageView mYooAvatar;

        public ChatsAdapter(Context context, int textViewResourceId, ArrayList<Chat> items)
        {
            super(context, textViewResourceId, items);
            this.mListOfChats = items;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent)
        {
            mChat = mListOfChats.get(position);
            mView = convertView;

            if (mView == null)
            {
                mLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                mView = mLayoutInflater.inflate(R.layout.chats_all_list_item_chat_with_one_yoo, null);
            }

            if (mChat != null)
            {

                mYooAvatar= (ImageView) mView.findViewById(R.id.chat_all_yoo_avatar);
                mNickname = (TextView) mView.findViewById(R.id.single_chat_nick);
                mSomeChatText = (TextView) mView.findViewById(R.id.single_chat_text);

                if (mYooAvatar!=null){
                    if (mChat.getYoos().size()!=1){
                        mYooAvatar.setImageDrawable(getResources().getDrawable(R.drawable.social_group));
                    }
                }
                
                if (mNickname != null)
                {
                    mNickname.setText(mChat.getChatName());
                }

                if (mSomeChatText != null)
                {
                    mSomeChatText.setText(mChat.getText());
                }

            }

            mView.setOnClickListener(new OnClickListener()
            {

                @Override
                public void onClick(View v)
                {
                    Chat chat= mList.get(position);
                    Intent intent = new Intent(ChatsAllActivity.this, ChatsNowActivity.class);
                    intent.putExtra(ChatsNowActivity.CHATS_NOW_CHAT, chat);
                    Log.v("FUUUUUU", ChatsTabActivity.group.toString());
                    
                    ChatsTabActivity.group.replaceContentView("ChatsNowActivity", intent);

                }
            });

            return mView;

        }
    }
}
